//
//  ViewController.m
//  BooksViewTable
//lkmn 
//  Created by mac on 15-12-10.
//  Copyright (c) 2015年 yb. All rights reserved.
//

#import "ViewController.h"
#import "DelegateViewController.h"
@interface ViewController ()
{
    NSArray *imgs;
    NSArray *books;
}
<<<<<<< HEAD

=======
>>>>>>> 475f81e3bc27cc7832b67614584a5dc39e5e8d10
@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
<<<<<<< HEAD
    //标题
    self.title = @"迷你书屋";
    //
    self.tableView.showsVerticalScrollIndicator = NO;
    //取读文件图片、txt文本
=======
    self.title = @"迷你书屋";
    self.tableView.showsVerticalScrollIndicator = NO;
>>>>>>> 475f81e3bc27cc7832b67614584a5dc39e5e8d10
    NSBundle *boudle = [NSBundle mainBundle];
    imgs = [boudle URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    NSLog(@"imgs = %@",imgs);
    books = [boudle URLsForResourcesWithExtension:@"txt" subdirectory:nil];
    NSLog(@"books = %@",books);
<<<<<<< HEAD
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
=======
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!imgs && imgs == 0) {
        return 0;
    }
    NSUInteger count = imgs.count -1;
    return count/3 + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 139;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //书本摆放的位置
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    NSInteger index = row * 3;
    
//    for (NSURL *imgName in imgs) {
//        NSMutableArray *arr = [NSMutableArray array];
//        [arr addObject:[UIImage imageNamed:imgName.lastPathComponent]];
//        UIImage *image = [UIImage imageNamed:@"BookShelfCell"];
//        UIColor *color = [UIColor colorWithPatternImage:image];
//        self.view.backgroundColor =color;
//    }
    //书的排列位置
    for (int i = 0; i < 3 && index + i < imgs.count; i++) {
        UIImage *ag = [UIImage imageNamed:[imgs[index + i]lastPathComponent]];
        UIImageView *iv = [[UIImageView alloc]initWithImage:ag];
        CGFloat edge = 15;
        CGFloat width = (self.view.frame.size.width - 4 * edge) /3;
        iv.frame =  CGRectMake(edge + (edge + width)*i,8,width,139 - 8 - 8*2);
        [cell addSubview:iv];
        //添加按钮，连接小说
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = iv.bounds;
        btn.tag = 100 + index + i;
        [btn addTarget:self action:@selector(ochick:) forControlEvents:UIControlEventTouchUpInside];
        [iv addSubview:btn];
        iv.userInteractionEnabled = YES;
    }
    //书架的显示
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BookShelfCell.png"]];
    return cell;
}

- (void)ochick:(UIButton *)sender
{
    
    //点击
    NSInteger ger = sender.tag - 100;
    NSURL *imgUrl = imgs[ger];
    NSError * err;
    NSString *name = imgUrl.lastPathComponent;
    name = [name stringByReplacingCharactersInRange:NSMakeRange(name.length - 5, 5) withString:@""];
    NSURL *url;
    for (NSURL *nsurl in books) {
        if ([nsurl.absoluteString rangeOfString:name].location != NSNotFound) {
            url = nsurl;
            break;
        }
    }
    NSString *cont = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
    NSLog(@"%@",cont);
    DelegateViewController *con =[[DelegateViewController alloc]init];
    con.text = cont;
    [self.navigationController pushViewController:con animated:YES];

}

- (void)didReceiveMemoryWarninerrg
>>>>>>> 475f81e3bc27cc7832b67614584a5dc39e5e8d10
{
    //行的控制
    if (!imgs && imgs.count == 0) {
        return 0;
    }
    NSUInteger count = imgs.count -1;
    return count/3 + 1;
}

<<<<<<< HEAD
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //一共多少行
    return 139;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //书本摆放的位置
    NSInteger row = indexPath.row;
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    NSInteger index = row * 3;
    
//    for (NSURL *imgName in imgs) {
//        NSMutableArray *arr = [NSMutableArray array];
//        [arr addObject:[UIImage imageNamed:imgName.lastPathComponent]];
//        UIImage *image = [UIImage imageNamed:@"BookShelfCell"];
//        UIColor *color = [UIColor colorWithPatternImage:image];
//        self.view.backgroundColor =color;
//    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BookShelfCell.png"]];
    
    //书的排列位置
    for (int i = 0; i < 3 && index + i < imgs.count; i++) {
        UIImage *ag = [UIImage imageNamed:[imgs[index + i]lastPathComponent]];
        UIImageView *iv = [[UIImageView alloc]initWithImage:ag];
        CGFloat edge = 15;
        CGFloat width = (self.view.frame.size.width - 4 * edge) /3;
        iv.frame =  CGRectMake(edge + (edge + width)*i,8,width,139 - 8 - 8*2);
        [cell addSubview:iv];
        //添加按钮，连接小说
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = iv.bounds;
        btn.tag = 100 + index + i;
        [btn addTarget:self action:@selector(ochick:) forControlEvents:UIControlEventTouchUpInside];
        [iv addSubview:btn];
        iv.userInteractionEnabled = YES;
    }
    //书架的显示

    return cell;
}

- (void)ochick:(UIButton *)sender
{
    
    //点击
    NSInteger ger = sender.tag - 100;
    NSURL *imgUrl = imgs[ger];
    NSError * err;
    NSString *name = imgUrl.lastPathComponent;
    name = [name stringByReplacingCharactersInRange:NSMakeRange(name.length - 5, 5) withString:@""];
    NSURL *url;
    for (NSURL *nsurl in books) {
        if ([nsurl.absoluteString rangeOfString:name].location != NSNotFound) {
            url = nsurl;
            break;
        }
    }
    NSString *cont = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
    if (err) {
        NSLog(@"%@",err.description);
    }
    DelegateViewController *con =[DelegateViewController new];
    con.text = cont;
    //[self.navigationController pushViewController:con animated:YES];
    //进阅读界面的翻转
    [con setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentModalViewController:con animated:YES];
}

/*
- (void)viewWillAppear:(BOOL)animated
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
    //打开状态栏
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    //打开导航栏
    // self.navigationController.navigationBar.hidden = NO;
    [UIView commitAnimations];
    return;
}
*/
- (void)didReceiveMemoryWarninerrg
{
    [super didReceiveMemoryWarning];
     //Dispose of any resources that can be recreated.
}
@end
=======
@end  
>>>>>>> 475f81e3bc27cc7832b67614584a5dc39e5e8d10
